home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15260 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  88 lines

  1. Newsgroups: comp.lang.c++
  2. Path: in1.uu.net!shore!mv!usenet
  3. From: ENGR@GSSI.MV.COM (Michael Furman)
  4. Subject: Re: Constructor member initializer list
  5. Message-ID: <DpCE37.DpE@mv.mv.com>
  6. Mime-Version: 1.0
  7. Content-Type: Text/Plain; charset=US-ASCII
  8. Organization: GSSI
  9. Date: Thu, 4 Apr 1996 14:40:18 GMT
  10. References: <4jua8k$fa1$2@mhadg.production.compuserve.com>
  11. X-Newsreader: WinVN 0.99.7
  12. X-Nntp-Posting-Host: gssi.mv.com
  13.  
  14. In article <4jua8k$fa1$2@mhadg.production.compuserve.com>, 
  15. 74312.2300@CompuServe.COM says...
  16. >
  17. >How do you initialize class members in the initializer list if the 
  18. >member is a structure?
  19.  
  20. I think you mean aggregate class - not structure. There in no difference 
  21. between structure and class in C++ except default "public:"
  22. at the begining of structure. Aggregate class is a class with no 
  23. constructors, private or protected members, base classes or virtual
  24. functions.
  25.  
  26. >
  27. >Consider the following code fragment.
  28. >
  29. >typedef struct _tagRect {
  30. >   int      x;
  31. >   int      y;
  32. >   int      w;
  33. >   int      h;
  34. >} RECTANGLE;
  35. >
  36. >class Foo {
  37. >   Foo() : <how do I initialize fooRect and it's members> {};
  38. >   RECTANGLE   fooRect;
  39. >};
  40.  
  41. You can not do it if RECTANGLE is an aggregate, because constructor
  42. initializer
  43. can not use form {....}. You have two choices: add constructor to your
  44. structure or put initialization inside constructor body using assignment
  45. statements.
  46.  
  47. And your typedef is redundant - you can write:
  48.  
  49. //=== Variant 1
  50. struct RECTANGLE {
  51.   int      x;
  52.   int      y;
  53.   int      w;
  54.   int      h;
  55. };
  56.  
  57. class Foo {
  58.    RECTANGLE   fooRect;
  59.    Foo() {foorect.x = 1; ........ }
  60. };
  61.  
  62. //===== or:
  63. //=== Variant 2
  64. struct RECTANGLE {
  65.   int      x;
  66.   int      y;
  67.   int      w;
  68.   int      h;
  69.   RECTANGLE(int x_, int y_, int w_, int h_) : x(x_), y(y_), w(w_), h(h_) {}
  70. };
  71.  
  72. class Foo {
  73.    RECTANGLE   fooRect;
  74.    Foo() : fooRect(1, 2, 3, 4) {}
  75. };
  76.  
  77. I did not test this code fragments - they may contain tupos.
  78.  
  79. -- 
  80. <<< If you received it by E-mail: it is a copy of post to the newsgroup >>>
  81. ---------------------------------------------------------------
  82. Michael Furman,                       (603)893-1109
  83. Geophysical Survey Systems, Inc.  fax:(603)889-3984
  84. 13 Klein Drive - P.O. Box 97          engr@gssi.mv.com 
  85. North Salem, NH 03073-0097            71543.1334@compuserve.com
  86. ---------------------------------------------------------------
  87.  
  88.